package design.Flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by looper on 2017/8/17.
 */
public class FlyweightFactory {
  private Map<String,Flyweight> flyweightMap = new HashMap<>();

  public Flyweight factory(String key){
    //如果共享对象的工厂中有该对象就直接返回该对象
    Flyweight flyweight = flyweightMap.get(key);
    if(flyweight == null){
      //如果共享对象的工厂中没有该对象就会自动创建一个
      flyweight = new ConcreteFlyweight(key);
      flyweightMap.put(key,flyweight);
    }
    return flyweight;
  }


  //查看一共有多少个对象
  public void getMapSize(){
    System.out.println("size = "+flyweightMap.size());
  }
}
